/**
 * Copyright (c) 2018-2022, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: process asm
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2022-05-20     JasonHu           Init
 */

#define __ASSEMBLY__

/*********************
 *      SPSR BIT     *
 *********************/

#define  SPSR_Mode(v)          ((v) << 0)
#define  SPSR_A64              (0 << 4)
#define  SPSR_RESEVRED_5       (0 << 5)
#define  SPSR_FIQ_MASKED(v)    ((v) << 6)
#define  SPSR_IRQ_MASKED(v)    ((v) << 7)
#define  SPSR_SERROR_MASKED(v) ((v) << 8)
#define  SPSR_D_MASKED(v)      ((v) << 9)
#define  SPSR_RESEVRED_10_19   (0 << 10)
#define  SPSR_IL(v)            ((v) << 20)
#define  SPSR_SS(v)            ((v) << 21)
#define  SPSR_RESEVRED_22_27   (0 << 22)
#define  SPSR_V(v)             ((v) << 28)
#define  SPSR_C(v)             ((v) << 29)
#define  SPSR_Z(v)             ((v) << 30)
#define  SPSR_N(v)             ((v) << 31)

/*
 * void NX_HalProcessEnterUserMode(args, text, userStack, returnAddr);
 */
.global NX_HalProcessEnterUserMode
NX_HalProcessEnterUserMode:
    mov x4, #(SPSR_Mode(0) | SPSR_A64)
    msr daifset, #3
    dsb sy
    msr spsr_el1, x4
    msr elr_el1, x1
    eret

.global NX_HalUserThreadReturnCodeBegin
NX_HalUserThreadReturnCodeBegin:
    mov x0, #0    // x0 = exit code 
    mov x8, #48   // x8 = 48, SysThreadExit
    svc #0

.align 3
.global NX_HalUserThreadReturnCodeEnd
NX_HalUserThreadReturnCodeEnd:

.align 3
.global NX_HalSignalReturnCodeBegin
NX_HalSignalReturnCodeBegin:
    mov x8, 0xF00000   // x8 = SysSignalReturn, hide for user
    svc #0

.align 3
.global NX_HalSignalReturnCodeEnd
NX_HalSignalReturnCodeEnd:
